package com.se.common;

import com.se.exception.FileTooLongException;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.*;
import java.nio.file.Path;

public class FileMessage extends Message implements Serializable, SpecialStyleMessage{
    @Serial
    private static final long serialVersionUID = 1L;

    private String sendUser;

    private String receiveUser;

    private byte[] fileContent;

    private int leftIndex = -1;

    private int rightIndex = -1;

    private String filePath;

    private String msgType;

    private String sendTime;

    public FileMessage() {
    }

    public FileMessage(int left, int right, String filePath) {
        leftIndex = left;
        rightIndex = right;
        this.filePath = filePath;
    }

    @Override
    public String getSendTime() {
        return sendTime;
    }

    @Override
    public void setSendTime(String sendTime) {
        if(sendTime == null) {
            return;
        }
        this.sendTime = sendTime;
    }

    public void load() throws FileTooLongException {

        if(new File(filePath).length() > Integer.MAX_VALUE) {
            throw new FileTooLongException("文件过大！");
        }

        int size = (int) new File(filePath).length();
        fileContent = new byte[size];

        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            fileInputStream.read(fileContent);

        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public void load(String path) throws FileTooLongException{
        if(new File(path).length() > Integer.MAX_VALUE) {
            throw new FileTooLongException("文件过大！");
        }

        int size = (int) new File(path).length();
        fileContent = new byte[size];

        try (FileInputStream fileInputStream = new FileInputStream(path)) {
            fileInputStream.read(fileContent);
        } catch (IOException e) {
            /*
            异常处理待优化
             */
            e.printStackTrace();
        }
    }

    public String getFilePath() {
        return filePath;
    }

    @Override
    public int getRightIndex() {
        return rightIndex;
    }

    @Override
    public int getLeftIndex() {
        return leftIndex;
    }

    @Override
    public void setMsgType(String type) {
        msgType = type;
    }

    @Override
    public String getMsgType() {
        return msgType;
    }

    @Override
    public String getSendUser() {
        return sendUser;
    }

    @Override
    public void setSendUser(String sendUser) {
        if(sendUser == null) {
            return;
        }
        this.sendUser = sendUser;
    }

    @Override
    public String getReceiveUser() {
        return receiveUser;
    }

    @Override
    public void setReceiveUser(String receiveUser) {
        if(receiveUser == null) {
            return;
        }
        this.receiveUser = receiveUser;
    }

    @Override
    public void shiftIndex(int offset) {
        leftIndex += offset;
        rightIndex += offset;
    }

    public byte[] getFileContent() {
        return fileContent;
    }

    public void setFileMessage(Message message) {
        sendUser = message.getSendUser();
        receiveUser = message.getReceiveUser();
        sendTime = message.getSendTime();
    }

    public FileMessage unLoad() {
        FileMessage unLoadFile = new FileMessage();
        unLoadFile.setFileMessage(this);
        unLoadFile.setContent(filePath);
        unLoadFile.setMsgType(msgType);
        return unLoadFile;
    }

    public String getFileName() {
        Path path = Path.of(filePath);
        return path.getFileName().toString();
    }

    //为了兼容后续数据库操作
    @Override
    public String getContent() {
        return filePath;
    }

    @Override
    public void setContent(String content) {
        filePath = content;
    }

    @Override
    public String getId() {
        return DigestUtils.sha256Hex(filePath);
    }
}
